package wh.coach._datainput;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author hangwen
 * @date 2023/7/13
 */
public class Main {
    public static void main(String[] args) throws IOException {
        final File parent = new File(System.getProperty("user.dir"));

        final File file3 = new File(parent, "file3.txt");
        file3.createNewFile();

        try (FileOutputStream fos = new FileOutputStream(file3, true)) {
            final DataOutputStream dataOutputStream = new DataOutputStream(new BufferedOutputStream(fos));

            String msg = "你是个猪头!";
            final byte[] data = msg.getBytes();

            dataOutputStream.writeInt(data.length);
            dataOutputStream.write(data);

            dataOutputStream.flush();
        }

        try (FileInputStream fis = new FileInputStream(file3)) {
            final DataInputStream dataInputStream = new DataInputStream(new BufferedInputStream(fis));

            while (dataInputStream.available() > 0) {
                final int len = dataInputStream.readInt();
                byte[] data = new byte[len];
                dataInputStream.readFully(data);

                System.out.println(new String(data));
            }

        }
    }
}
